package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.AccountException;
import com.management.commons.exception.ExceptionCode.AccountExceptionCode;
import com.management.controller.form.AccountForm;
import com.management.controller.form.AddAccountForm;
import com.management.controller.form.ChangeAccountForm;
import com.management.controller.form.GetAccountByNameForm;
import com.management.controller.info.AccountPageInfo;
import com.management.controller.info.GetAccountByNameInfo;
import com.management.model.Account;
import com.management.dao.AccountDao;
import com.management.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  账户相关业务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {

    @Resource
    private AccountDao accountDao;

    /**
     * 分页查询所有账户
     * @param accountForm 分页参数
     * @return
     */
    @Override
    public Result findAllAccount(AccountForm accountForm) {
        AccountForm page = page(accountForm);
        List list = CopyUtils.CopyList(page.getRecords(), AccountPageInfo.class);
        Page<AccountPageInfo> infoPage=new Page<>();
        infoPage.setSize(page.getSize());
        infoPage.setTotal(page.getTotal());
        infoPage.setRecords(list);
        infoPage.setPages(page.getPages());
        infoPage.setCurrent(page.getCurrent());

        return Result.getInstance(Page.class).setData(infoPage);
    }

    @Override
    public Result changeMessage(ChangeAccountForm changeAccountForm) {
       Integer num= accountDao.changeMessage(changeAccountForm);
       if(num==1){
           return Result.getInstance().setCode(200).setMessage("修改成功");
       }throw  new AccountException(AccountExceptionCode.CHANGEMESSAGE_ERROR);

    }

    @Override
    public Result addAccount(AddAccountForm addAccountForm) {
        Account account = CopyUtils.CopyOne(addAccountForm, Account.class);
        boolean save = save(account);
        if(save){
            return Result.getInstance().setCode(200).setMessage("添加成功");
        }
        throw new AccountException(AccountExceptionCode.ADDACCOUNT_ERROR);
    }

    @Override
    public Result findByAccountName(GetAccountByNameForm getAccountByNameForm) {
        if(getAccountByNameForm.getAccountName()==null||getAccountByNameForm.getAccountName().equals("")){
            throw new AccountException(AccountExceptionCode.GETACCOUNTBYNAME_ERROR);
        }
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("account_name",getAccountByNameForm.getAccountName());
        GetAccountByNameForm page = page(getAccountByNameForm, queryWrapper);
        List<GetAccountByNameInfo> list = CopyUtils.CopyList(page.getRecords(), GetAccountByNameInfo.class);
        Page<GetAccountByNameInfo> infoPage=new Page<>();
        infoPage.setRecords(list);
        infoPage.setSize(page.getSize());
        infoPage.setTotal(page.getTotal());
        infoPage.setCurrent(page.getCurrent());
        infoPage.setPages(page.getPages());
        return Result.getInstance(Page.class).setData(infoPage);
    }




}
